//
//  CommentView.m
//  Lmm
//
//  Created by Han on 16/6/24.
//  Copyright © 2016年 Lingmaimai. All rights reserved.
//

#import "CaptchaImageView.h"
#import "Global.h"
#import "BRPlaceholderTextView.h"
#import "Tool.h"
#import "PINCache.h"

#define TIME_INTERVAL 0.4f

typedef void(^Block)(NSString *captcha);

@implementation CaptchaImageView {
    
    BRPlaceholderTextView *_contentTextView;
    
    UIButton *maskView;
    
    Block _block;
}

- (id) init:(void(^)(NSString *content))block {
    _block = block;
    
    self = [super initWithFrame:CGRectMake(0, 0, MAIN_SCREEN_WIDTH, MAIN_SCREEN_HEIGHT)];
    
    if (self) {
        [self drawView];
    }
    
    return self;
}

- (void) confirm:(UIButton *)button {
    
    
    NSString *content = [_contentTextView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    if (content.length == 0) {
        [Tool showHUD:@"图片验证码不能为空"  : self];
        return;
    }
    
    [self hiddenView];
    
    if (_block) {
        _block(content);
    }
    
}

- (void) drawView {
    maskView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, MAIN_SCREEN_WIDTH, MAIN_SCREEN_HEIGHT)];
    maskView.backgroundColor = [UIColor blackColor];
    maskView.alpha = 0.0;
    [maskView addTarget:self action:@selector(hiddenView) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:maskView];
    
    [UIView animateWithDuration:TIME_INTERVAL animations:^{
      self->maskView.alpha = 0.4;
        
    }];
    
    
    
    float captchaViewWidth = MAIN_SCREEN_WIDTH - 40;
    
    UIView *captchaView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, captchaViewWidth, 170)];
    captchaView.backgroundColor = [UIColor whiteColor];
    captchaView.alpha = 1.0;
    captchaView.clipsToBounds = YES;
    captchaView.layer.cornerRadius = 5;
    captchaView.center = maskView.center;
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, captchaViewWidth, 38)];
    titleLabel.font = [UIFont fontWithName:MAIN_FONT_NAME size:18];
    titleLabel.textColor = MAIN_FONT_COLOR;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = @"请输入图形验证码";
    [captchaView addSubview:titleLabel];
    
    _contentTextView = [[BRPlaceholderTextView alloc] initWithFrame:CGRectMake(10, 40, captchaViewWidth - 20, 36)];
    _contentTextView.backgroundColor = UIColorFromRGB(0xF0F0F0);
    _contentTextView.font = [UIFont fontWithName:MAIN_FONT_NAME size:18];
    _contentTextView.textColor = MAIN_FONT_COLOR;
    _contentTextView.placeholder = @"请输入右侧验证码";
    [_contentTextView setPlaceholderColor:SECOND_FONT_COLOR];
    [_contentTextView setPlaceholderOpacity:0.6];
    [captchaView addSubview:_contentTextView];
    
    self->_captchaImage = [[UIImageView alloc] initWithFrame:CGRectMake(captchaViewWidth - 110, 40, 100, 36)];
    self->_captchaImage.contentMode = UIViewContentModeScaleAspectFill;
    self->_captchaImage.clipsToBounds = YES;
    self->_captchaImage.userInteractionEnabled = YES;
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(loadImageUrl)];
    [self->_captchaImage addGestureRecognizer:singleTap];
    [captchaView addSubview:self->_captchaImage];
    
    UILabel *hintLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 78, captchaViewWidth - 20, 36)];
    hintLabel.font = [UIFont fontWithName:MAIN_FONT_NAME size:14];
    hintLabel.textColor = MAIN_FONT_COLOR;
    hintLabel.text = @"*如果图片验证码识别困难，点击即可刷新显示";
    [captchaView addSubview:hintLabel];
    
    int buttonWidth = (captchaViewWidth - 80) / 2;
    UIButton *cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 120, buttonWidth, 36)] ;
    cancelButton.layer.cornerRadius = 5;
    cancelButton.backgroundColor = UIColorFromRGB(0xF0F0F0);
    cancelButton.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 10);
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton setTitleColor:SECOND_FONT_COLOR forState:UIControlStateNormal];
    cancelButton.titleLabel.font = [UIFont fontWithName:MAIN_FONT_NAME size:18];
    [cancelButton addTarget:self action:@selector(hiddenView) forControlEvents:UIControlEventTouchUpInside];
    [captchaView addSubview:cancelButton];
    
    UIButton *confirmButton = [[UIButton alloc] initWithFrame:CGRectMake(captchaViewWidth - buttonWidth - 10, 120, buttonWidth, 36)] ;
    confirmButton.layer.cornerRadius = 5;
    confirmButton.backgroundColor = MAIN_COLOR;
    confirmButton.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 10);
    [confirmButton setTitle:@"确定" forState:UIControlStateNormal];
    [confirmButton setTitleColor:UIColorFromRGB(0xFFFFFF) forState:UIControlStateNormal];
    confirmButton.titleLabel.font = [UIFont fontWithName:MAIN_FONT_NAME size:18];
    [confirmButton addTarget:self action:@selector(confirm:) forControlEvents:UIControlEventTouchUpInside];
    [captchaView addSubview:confirmButton];
    
    [self addSubview:captchaView];
    //[self exChangeOut:captchaView dur:TIME_INTERVAL];
    
    [self loadImageUrl];
    
}

- (void) loadImageUrl {
    

    
    UserModel* userModel = [[PINCache sharedCache] objectForKey:@"user"];
    
    NSString *captchaImageUrl = [NSString stringWithFormat:@"http://%@/app/captcha?token=%@", SERVER_URL, userModel.token];
    //NSLog(@"captchaImageUrl==%@", captchaImageUrl);
    
    [self->_captchaImage sd_setImageWithURL:[NSURL URLWithString:captchaImageUrl] placeholderImage:nil options:SDWebImageRefreshCached];
}


- (void)hiddenView {
    [UIView animateWithDuration:TIME_INTERVAL animations:^{
        self->maskView.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)exChangeOut:(UIView *)changeOutView dur:(CFTimeInterval)dur {
    CAKeyframeAnimation * animation;
    animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = dur;
    //animation.delegate = self;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    NSMutableArray *values = [NSMutableArray array];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 0.9)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    animation.values = values;
    animation.timingFunction = [CAMediaTimingFunction functionWithName: @"easeInEaseOut"];
    [changeOutView.layer addAnimation:animation forKey:nil];
}






@end
